package com.kai.steering.data.service.ctl;

import com.kai.steering.data.service.entity.domainObject.UserDO;
import com.kai.steering.data.service.entity.dto.UserQueryDTO;
import com.kai.steering.data.service.entity.po.User;
import com.kai.steering.data.service.entity.vo.UserVO;
import com.kai.steering.data.service.service.UserService;
import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.ctl.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import javax.validation.Valid;

import com.kai.steering.common.vo.Page;
import com.kai.steering.common.web.vo.ReturnCodeExt;

import java.util.List;


/**
 * <pre>
 * 用户 前端控制器
 * </pre>
 *
 * @author kai
 * @since 2022-02-23
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户 API")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;


    @PostMapping
    @ApiOperation(value = "用户-添加")
    public Result<Boolean> addUser(@Valid @RequestBody UserQueryDTO userQueryDTO) {

        /**
         * 参数检查
         */
        if (StringUtils.isEmpty(userQueryDTO.getUserId()) || StringUtils.isEmpty(userQueryDTO.getUserName()) ||
                StringUtils.isEmpty(userQueryDTO.getPassword())) {
            return Result.failed("参数不完整");
        }

        /**
         * 构建业务对象
         */
        UserDO userDO = new UserDO(userQueryDTO);

        /**
         * 调用服务
         */
        userDO = userService.saveUser(userDO);

        /**
         * 构建视图对象
         */
        if (userDO == null || StringUtils.isNotEmpty(userDO.getUserId())) {
            return Result.failed();
        }

        return Result.ok();
    }


    @PutMapping("/{id}")
    @ApiOperation(value = "用户-编辑")
    public Result<Boolean> updateUser(@PathVariable("id") Serializable id, @Valid @RequestBody UserQueryDTO userQueryDTO) {
        /**
         * 参数检查
         */
        //id
        if (StringUtils.isNotEmpty(id.toString())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("对象[" + id + "]为空");
        }
        //属性
        if (StringUtils.isNotEmpty(userQueryDTO.getUserName()) || StringUtils.isNotEmpty(userQueryDTO.getPassword())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("属性为空");
        }
        /**
         * 构建业务对象
         */
        UserDO userDO = new UserDO(userQueryDTO);
        /**
         * 调用服务
         */
        boolean b = userService.updateUser(id, userDO);

        /**
         * 构建视图对象
         */
        return Result.ok(b);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "用户-删除")
    public Result<Boolean> deleteUser(@PathVariable("id") String id) {
        /**
         * 参数检查
         */
        //id
        if (StringUtils.isEmpty(id.toString())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("对象[" + id + "]为空");
        }
        /**
         * 调用服务
         */
        boolean flag = userService.deleteUser(id);


        return Result.ok(flag);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "用户-详情")
    public Result<UserVO> getUser(@PathVariable("id") Serializable id) {
        /**
         * 参数检查
         */
        //id
        if (StringUtils.isEmpty(id.toString())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("对象[" + id + "]为空");
        }
        /**
         * 调用服务
         */
        UserDO userDO = userService.getUserById(id,true);
        if (userDO == null || StringUtils.isEmpty(userDO.getUserId())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("id[" + id + "]对象不存在");
        }

        /**
         * 构建视图对象
         */
        UserVO userVO = new UserVO(userDO);
        return Result.ok(userVO);
    }


    @PostMapping("/list")
    @ApiOperation(value = "用户-综合查询")
    public Result<Page<UserVO>> getUserPageList(@Valid @RequestBody UserQueryDTO userQueryDTO) {
        /**
         * 参数验证
         */
        //创建时间  都为空 或（都不为空且开始<=结束）
        if ((userQueryDTO.getCreationDateOrigin() == null && userQueryDTO.getCreationDateTerminus() == null) ||
                ((userQueryDTO.getCreationDateOrigin() != null && userQueryDTO.getCreationDateTerminus() != null) &&
                        userQueryDTO.getCreationDateOrigin().getTime() <= userQueryDTO.getCreationDateTerminus().getTime())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("创建时间查询参数错误");
        }
        //生日时间
        if ((userQueryDTO.getBirthdayDateOrigin() == null && userQueryDTO.getBirthdayDateTerminus() == null) ||
                ((userQueryDTO.getBirthdayDateOrigin() != null && userQueryDTO.getBirthdayDateTerminus() != null) &&
                        userQueryDTO.getBirthdayDateOrigin().getTime() <= userQueryDTO.getBirthdayDateTerminus().getTime())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("生日时间查询参数错误");
        }
        Page<UserVO> paging = userService.getUserPageList(userQueryDTO);
        return Result.ok(paging);
    }


    @GetMapping("/list")
    @ApiOperation(value = "用户-全部列表")
    public Result<List<UserVO>> getUserAllList() {

        List<UserVO> list = userService.getAllUser();
        return Result.ok(list);
    }


}